package com.lzkj.baotouhousingfund.ui.fragment;

import android.app.AlertDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.coderc.library.utils.LogUtil;
import com.coderc.library.view.ToastUtil;
import com.lzkj.baotouhousingfund.R;
import com.lzkj.baotouhousingfund.model.bean.CenterIntroductionBean;
import com.lzkj.baotouhousingfund.presenter.GovernmentAffairsNewsPresenter;
import com.lzkj.baotouhousingfund.presenter.contract.GovernmentAffairsNewsContract;
import com.lzkj.baotouhousingfund.widget.recyclerview.XRecyclerLinearFragment;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.OnClick;


/**
 * @author lx
 * @description 政务新闻
 * @date 2018-3-13 10:10:32
 */

public class GovernmentAffairsNewsFragment extends XRecyclerLinearFragment<GovernmentAffairsNewsPresenter,CenterIntroductionBean>
        implements GovernmentAffairsNewsContract.View, BaseQuickAdapter.OnItemChildClickListener {

    //分页码
    private int     mStart       = 0;
    //页码内容数
    private int     mLimit       = 10;
    //刷新
    private boolean isRefreshing = false;
    //读取更多
    private boolean isLoading    = false;
    //新闻类型
    private int mType = 1;

    private List<CenterIntroductionBean> mCenterIntroductionBeans = new ArrayList<>();

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

    }

    @Override
    public void showError(String msg) {
        LogUtil.e(msg);
        ToastUtil.showShort(msg);
        finishRefresh();
        finishLoadmore();
        setErrorView();
        setEnableRefresh(true);
        setEnableLoadMore(true);
        isLoading = false;
        isRefreshing = false;
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    public void setType(int type) {
        mType = type;
    }

    @Override
    protected void initEventAndData() {

    }


    //============================RecyclerView Refresh============================

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        super.onRefresh(refreshLayout);
        setEnableLoadMore(false);
        if (isRefreshing) {
            return;
        }
        mStart = 0;
        fetchNetWork();
        isRefreshing = true;
    }

    //============================RecyclerView LoadMore============================

    @Override
    public void onLoadmore(RefreshLayout refreshLayout) {
        super.onLoadmore(refreshLayout);
        if (isLoading) {
            return;
        }
        //start从0开始,需减1
        mStart = mAdapter.getData().size() - 1;
        fetchNetWork();
        setEnableRefresh(false);
        isLoading = true;
    }
    //============================fetch news data============================

    private void fetchNetWork() {
        showList(mCenterIntroductionBeans);
//        mPresenter.fetch(SharedPreferenceUtil.getUserId(), mStart, mLimit);
    }

    @Override
    public void showList(List<CenterIntroductionBean> list) {
        if (isRefreshing) {
            setLoadmoreFinished(false);
            mAdapter.setNewData(list);
            finishRefresh();
            setEnableLoadMore(true);
            isRefreshing = false;
        }
        if (isLoading) {
            mAdapter.addData(list);
            finishLoadmore();
            setEnableRefresh(true);
            isLoading = false;
        }
        if (list.size() < mLimit) {
            setLoadmoreFinished(true);
        }

        if (mAdapter.getData().size() == 0) {
            setNotDataView();
            finishRefresh();
        }
    }

    @Override
    protected int getItemLayoutId() {
        return R.layout.activity_center_introduction_item;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (mType==1){
            for (int i = 0; i < 10; i++) {
                CenterIntroductionBean bean = new CenterIntroductionBean();
                bean.date = "2018-03-" + (8 + i);
                bean.title = "包头市住房公积金管理中心最新动态" + i;
                mCenterIntroductionBeans.add(bean);
            }
        }
        if (mType==2){
            for (int i = 0; i < 10; i++) {
                CenterIntroductionBean bean = new CenterIntroductionBean();
                bean.date = "2018-03-" + (8 + i);
                bean.title = "包头市住房公积金管理中心本地新闻" + i;
                mCenterIntroductionBeans.add(bean);
            }
        }
        if (mType==3){
            for (int i = 0; i < 10; i++) {
                CenterIntroductionBean bean = new CenterIntroductionBean();
                bean.date = "2018-03-" + (8 + i);
                bean.title = "包头市住房公积金管理中心行业新闻" + i;
                mCenterIntroductionBeans.add(bean);
            }
        }
        if (mType==4){
            for (int i = 0; i < 10; i++) {
                CenterIntroductionBean bean = new CenterIntroductionBean();
                bean.date = "2018-03-" + (8 + i);
                bean.title = "包头市住房公积金管理中心新闻发布" + i;
                mCenterIntroductionBeans.add(bean);
            }
        }
        mAdapter.setNewData(mCenterIntroductionBeans);
    }

    @Override
    protected void convert(BaseViewHolder helper, CenterIntroductionBean item) {
        helper.setText(R.id.txt_title,item.title);
        helper.setText(R.id.txt_date,item.date);
    }


}
